package com.ws.shop.app;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import org.apache.log4j.Logger;

import com.ws.shop.util.CommonUtil;
import com.ws.shop.util.DateUtil;
import com.ws.shop.util.JdbcUtil;

public class AuctionHandler {

	private static Logger log = Logger.getLogger(AuctionHandler.class);
	private Connection connect;
	private String record;

	public AuctionHandler(Connection connect, String record) {
		this.connect = connect;
		this.record = record;
	}

	public void handler() throws SQLException {
		log.info("+++start+++");
		Record rec = parseString(record);
		updateAuction(rec);
		addpointc(rec);
	}

	/**
	 * 
	 * 解析字符串
	 * @param record
	 * uid|aid|name|goodsname|beat|pointb|ip|auctioncount|currentprice|currenttime
	 * 
	 */
	public Record parseString(String record){
		String[] recs = record.split("\\|");
		if(recs == null || recs.length <= 0)return null;
		Record rec = new Record();
		rec.setUid(CommonUtil.getLongValueByString(recs[0]));
		rec.setAid(CommonUtil.getLongValueByString(recs[1]));
		rec.setName(recs[2]);
		rec.setGoddsname(recs[3]);
		rec.setBeat(CommonUtil.getIntegerValueByString(recs[4]));
		rec.setPointb(CommonUtil.getIntegerValueByString(recs[5]));
		rec.setIp(recs[6]);
		rec.setAuctioncount(CommonUtil.getIntegerValueByString(recs[7]));
		rec.setCurrentprice(CommonUtil.getFloatValueByString(recs[8]));
		rec.setCurrenttime(CommonUtil.getLongValueByString(recs[9]));
		rec.setPointc(1);
		return rec;
	}
	
	/**
	 * 入竞拍日志表，修改竞拍信息
	 * @param rec
	 * @throws SQLException 
	 * 
	 */
	public int updateAuction(Record rec) throws SQLException {
		Statement stmt = connect.createStatement();
		float currentprice = rec.getCurrentprice();//当前价格
		String s = "insert into jvf_auction_log ( aid, uid, name, goodsname, beat, pointb, ip, address, bidprice, addtime) values("+rec.getAid()
		    +"," + rec.getUid() + ",'" + rec.getName() + "','" + rec.getGoddsname() + "'," + rec.getBeat() + "," + rec.getPointb() 
		    +",'" + rec.getIp() + "','" + rec.getIp() + "',"+currentprice+",now())";
		stmt.addBatch(s);
		long endtime = queryEndTime(rec.getAid());
		if(endtime < rec.getCurrenttime()){
			String cuttime = DateUtil.dateToString(new Date(rec.getCurrenttime()), DateUtil.YYYY_MM_DD_HH_MM_SS);
			s = "update jvf_auction set uid = "+rec.getUid()+" ,auctionuname='"+rec.getName()+"',auctioncount = "+rec.getAuctioncount()+", realprice = "+currentprice+", endtime = str_to_date('" + cuttime + "','%Y-%m-%d %k:%i:%s')  where id = " + rec.getAid();
			stmt.addBatch(s);
		}
        stmt.executeBatch();
        JdbcUtil.close(null, stmt, null);
        return 0;
	}

	public long queryEndTime(long aid) throws SQLException{
		String s = "select endtime from jvf_auction where id = " + aid;
		Statement stmt = connect.createStatement();
		ResultSet rs = stmt.executeQuery(s);
		long endtime = 0;
		if(rs.next()){
			endtime = rs.getTimestamp(1).getTime();
		}
		JdbcUtil.close(rs, stmt, null);
		return endtime;
	}
	
	/*
	 * String payment="参加竞拍"; String
	 * remark="参加竞拍,"+auctiondata.getGoodsname()+",点击1次,赠送"+
	 * auctiondata.getPointc()+"积分";
	 */
	public int addpointc(Record rec) throws SQLException {
		if (rec.getPointc() > 0) {
			Statement stmt = null;
			stmt = connect.createStatement();
			String s = "update jvf_user set pointc = pointc + " + rec.getPointc() + " where id = " + rec.getUid();
			stmt.addBatch(s);
			s = "insert into jvf_pointclog (uid, name, payment, money, addtime, remark)" + "values (" + rec.getUid()
					+ ", '" + rec.getName() + "', '" + rec.getPayment() + "', '" + rec.getPointc() + "', now(),'"
					+ rec.getRemark() + "')";
			stmt.addBatch(s);
			stmt.executeBatch();
			JdbcUtil.close(null, stmt, null);
			return 0;
		} else {
			return 1;
		}
	}

}

class Record {

	private long uid;
	private long aid;
	private String name;
	private String goddsname;
	private int beat;
	private int pointb;
	private int isfree;
	private String ip;
	private int auctioncount;
	private float currentprice;
	private long currenttime;
	private int pointc;
	private String remark;
	private String payment;

	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}

	public long getAid() {
		return aid;
	}

	public void setAid(long aid) {
		this.aid = aid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGoddsname() {
		return goddsname;
	}

	public void setGoddsname(String goddsname) {
		this.goddsname = goddsname;
	}

	public int getBeat() {
		return beat;
	}

	public void setBeat(int beat) {
		this.beat = beat;
	}

	public int getPointb() {
		return pointb;
	}

	public void setPointb(int pointb) {
		this.pointb = pointb;
	}

	public int getIsfree() {
		return isfree;
	}

	public void setIsfree(int isfree) {
		this.isfree = isfree;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getAuctioncount() {
		return auctioncount;
	}

	public void setAuctioncount(int auctioncount) {
		this.auctioncount = auctioncount;
	}

	public float getCurrentprice() {
		return currentprice;
	}

	public void setCurrentprice(float currentprice) {
		this.currentprice = currentprice;
	}

	public long getCurrenttime() {
		return currenttime;
	}

	public void setCurrenttime(long currenttime) {
		this.currenttime = currenttime;
	}

	public int getPointc() {
		return pointc;
	}

	public void setPointc(int pointc) {
		this.pointc = pointc;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getPayment() {
		return payment;
	}

	public void setPayment(String payment) {
		this.payment = payment;
	}

}